<?php
namespace app\mall\controller;

use app\data\controller\api\Auth;
use app\mall\logic\integralLogic;
use app\mall\logic\walletLogic;
use think\Exception;
use think\facade\Db;
use think\facade\Log;

/*
 * 积分中心
 * */
class Integral extends Auth{

    //积分提现兑换金币列表
    public function integral_list(){
        $type = request()->param('type',1);
        $logic = new integralLogic();
        $data = $logic->integral_list($type);
        $this->success('积分中心',$data);
    }

    //积分提现信息
    public function integral_withdraw_info(){
        $id = request()->param('id',0);
        $logic = new integralLogic();
        $res = $logic->integral_withdraw_info($this->user,$id);
        if ($res['code'] == 200) $this->success($res['msg'],$res['data']);
        $this->error($res['msg'],'',$res['code']);
    }

    //积分提现申请
    public function integral_withdraw_apply(){
        $id = request()->param('id',0);
        $type = request()->param('type',0);
        $account = request()->param('account','');
        $real_name = request()->param('real_name','');
        $logic = new integralLogic();
        $res = $logic->integral_withdraw_apply($this->user,$id,$type,$account,$real_name);
        if ($res['code'] == 200) $this->success($res['msg']);
        $this->error($res['msg'],'',$res['code']);
    }

    //积分提现记录
    public function integral_withdraw_list()
    {
        $page = request()->param('page',1);
        $limit = request()->param('limit',10);
        $logic = new integralLogic();
        $data = $logic->integral_withdraw_list($this->uuid,$page,$limit);
        $this->success('积分提现记录',$data);
    }

    //积分兑换金币信息
    public function integral_exchange_info(){
        $id = request()->param('id',0);
        $logic = new integralLogic();
        $res = $logic->integral_exchange_info($this->user,$id);
        if ($res['code'] == 200) $this->success($res['msg'],$res['data']);
        $this->error($res['msg'],'',$res['code']);
    }

    //积分提现申请
    public function integral_exchange_apply(){
        $id = request()->param('id',0);
        $logic = new integralLogic();
        $res = $logic->integral_exchange_apply($this->user,$id);
        if ($res['code'] == 200) $this->success($res['msg']);
        $this->error($res['msg'],'',$res['code']);
    }

    //积分兑换提现明细
    public function integral_change_list()
    {
        $page = request()->param('page',1);
        $limit = request()->param('limit',10);
        $logic = new integralLogic();
        $data = $logic->integral_change_list($this->uuid,$page,$limit);
        $this->success('积分兑换/提现记录',$data);
    }

    //用户积分变动明细
    public function user_integral_change_list()
    {
        $page = request()->param('page',1);
        $limit = request()->param('limit',10);
        $logic = new integralLogic();
        $data = $logic->user_integral_change_list($this->uuid,$page,$limit);
        $this->success('积分变更明细',$data);
    }

    //用户余额充值记录
    public function user_money_recharge_list()
    {
        $page = request()->param('page',1);
        $limit = request()->param('limit',10);
        $logic = new integralLogic();
        $data = $logic->user_money_recharge_list($this->uuid,$page,$limit);
        $this->success('余额变更明细',$data);
    }

    /**
     * 积分兑换金币-new
     */
    public function integralToMoney(){
       $integral_duihuan_num  = sysdata('integral_duihuan_num');
       $duihuan_money_num = sysdata('duihuan_money_num');
       $user = $this->user;
        //查看用户当前积分
        $user_integral =  Db::table('one_user_wallet')->where('u_id','=',$user['id'])->value('integral');

       if($user_integral<$integral_duihuan_num){
          $this->error('最少兑换积分是：'.$integral_duihuan_num);
       }

       $surplusIntegral =  $user_integral%$integral_duihuan_num;
       $duihuanIntegral = $user_integral-$surplusIntegral;  //消耗的积分
       $duihuanMoney = $duihuanIntegral/$integral_duihuan_num;  // 兑换的金币
        // 启动事务
        Db::startTrans();
        try{
            //更新积分变更记录
            walletLogic::wallet_detailed_add($user['id'],$duihuanIntegral,6,2,0,'reduce','积分兑换');
          //扣除用户积分
            Db::table('one_user_wallet')->where('u_id','=',$user['id'])->dec('integral',$duihuanIntegral)->update();
            //更新余额变更记录
            walletLogic::wallet_detailed_add($user['id'],$duihuanMoney,6,1,0,'add','积分兑换');
            //增加用户余额
            Db::table('one_user_wallet')->where('u_id','=',$user['id'])->inc('money',$duihuanMoney)->update();
            Db::commit();
            $this->success('兑换成功');

        }catch(Exception $e){
            var_dump($e->getMessage());exit;
            // 回滚事务
            Db::rollback();
            Log::error($e->getMessage());
        }

        $this->error('兑换失败');
    }




}